package top.frsf.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import top.frsf.reggie.common.R;
import top.frsf.reggie.dto.SetmealDto;
import top.frsf.reggie.entity.Category;
import top.frsf.reggie.entity.Setmeal;
import top.frsf.reggie.service.CategoryService;
import top.frsf.reggie.service.SetmealDishService;
import top.frsf.reggie.service.SetmealService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * @Author FRSF
 * @Date 2023/1/28 16:07
 * @Description 套餐控制层
 */

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;


    /**
     * @Author: FRSF
     * @Date: 2023/1/28 17:32
     * @Description 套餐分页查询
     * @param page:
     * @param pageSize:
     * @param name:
     * @return: top.frsf.reggie.common.R<java.util.List < top.frsf.reggie.dto.SetmealDto>>
     */
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(int page, int pageSize, String name) {
        // Setmeal分页对象
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        // SetmealDto分页对象
        Page<SetmealDto> setmealDtoPage = new Page<>();

        LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper<>();
        setmealWrapper.like(name != null, Setmeal::getName, name);
        setmealWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(setmealPage, setmealWrapper);

        // 分页数据拷贝，records不拷贝是因为泛型不同，需要手动配置
        BeanUtils.copyProperties(setmealPage, setmealDtoPage, "records");
        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> dtoRecords = new ArrayList<>();
        for (Setmeal record : records) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(record, setmealDto);

            // 分类ID转换为分类名称
            Long categoryId = record.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            dtoRecords.add(setmealDto);
        }

        setmealDtoPage.setRecords(dtoRecords);
        return R.success(setmealDtoPage);
    }

    /**
     * @Author: FRSF
     * @Date: 2023/2/5 15:50
     * @Description 根据条件查询套餐数据
     * @param setmeal:
     * @return: top.frsf.reggie.common.R<java.util.List<top.frsf.reggie.entity.Setmeal>>
     */
    @Cacheable(value = "setmealCache", key = "#setmeal.categoryId+':'+#setmeal.status")
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId, setmeal.getCategoryId());
        wrapper.eq(Setmeal::getStatus, setmeal.getStatus());
        List<Setmeal> list = setmealService.list(wrapper);
        return R.success(list);
    }


    /**
     * @Author: FRSF
     * @Date: 2023/1/28 17:05
     * @Description 新增套餐
     * @param setmealDto: 套餐dto
     * allEntries: 清理当前value下所有属性
     * @return: top.frsf.reggie.common.R<java.lang.String>
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        log.info("新增套餐信息:{}", setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }

    /**
     * @Author: FRSF
     * @Date: 2023/1/28 20:10
     * @Description 套餐起售/停售
     * @param status: 需要修改为的状态
     * @param ids: 套餐ID
     * @return: top.frsf.reggie.common.R<java.lang.String>
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable int status, Long ids) {
        log.info("销售id:{},销售状态:{}", ids, status);
        Setmeal setmeal = new Setmeal();
        setmeal.setId(ids);
        setmeal.setStatus(status);
        setmealService.updateById(setmeal);
        return R.success(status == 1 ? "起售成功" : "停售成功");
    }


    /**
     * @Author: FRSF
     * @Date: 2023/1/28 19:37
     * @Description 批量删除套餐，且关联菜品进行删除
     * @param ids:
     * allEntries: 清理当前value下所有属性
     * @return: top.frsf.reggie.common.R<java.lang.String>
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @DeleteMapping
    public R<String> deleteBatch(@RequestParam List<Long> ids) {
        log.info("删除套餐信息:{}", ids);
        setmealService.removeWithDish(ids);
        return R.success("删除套餐成功");
    }


}
